home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH10 / SRC / OBJEXTR.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  8.9 KB  |  305 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjExtrusion"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Private NumCurvePts As Integer
  11. Private NumPathPts As Integer
  12.  
  13. Private CurvePoints() As Point3D
  14. Private PathPoints() As Point3D
  15.  
  16. Private pline As ObjPolyline    ' Display polyline.
  17. ' ************************************************
  18. ' Add a point to the path.
  19. ' ************************************************
  20. Public Sub AddPathPoint(x As Single, y As Single, z As Single)
  21.     NumPathPts = NumPathPts + 1
  22.     ReDim Preserve PathPoints(1 To NumPathPts)
  23.     PathPoints(NumPathPts).coord(1) = x
  24.     PathPoints(NumPathPts).coord(2) = y
  25.     PathPoints(NumPathPts).coord(3) = z
  26.     PathPoints(NumPathPts).coord(4) = 1
  27. End Sub
  28.  
  29. ' ************************************************
  30. ' Add a point to the curve.
  31. ' ************************************************
  32. Public Sub AddCurvePoint(x As Single, y As Single, z As Single)
  33.     NumCurvePts = NumCurvePts + 1
  34.     ReDim Preserve CurvePoints(1 To NumCurvePts)
  35.     CurvePoints(NumCurvePts).coord(1) = x
  36.     CurvePoints(NumCurvePts).coord(2) = y
  37.     CurvePoints(NumCurvePts).coord(3) = z
  38.     CurvePoints(NumCurvePts).coord(4) = 1
  39. End Sub
  40.  
  41. ' ************************************************
  42. ' Create the display polyline.
  43. ' ************************************************
  44. Public Sub Extrude()
  45. Dim i As Integer
  46. Dim j As Integer
  47. Dim xoff1 As Single
  48. Dim yoff1 As Single
  49. Dim zoff1 As Single
  50. Dim xoff2 As Single
  51. Dim yoff2 As Single
  52. Dim zoff2 As Single
  53. Dim x1 As Single
  54. Dim y1 As Single
  55. Dim z1 As Single
  56. Dim x2 As Single
  57. Dim y2 As Single
  58. Dim z2 As Single
  59.  
  60.     Set pline = New ObjPolyline
  61.         
  62.     ' Create the translated images of the curve.
  63.     For i = 1 To NumPathPts
  64.         ' Calculate offsets for this path point.
  65.         xoff1 = PathPoints(i).coord(1) - PathPoints(1).coord(1)
  66.         yoff1 = PathPoints(i).coord(2) - PathPoints(1).coord(2)
  67.         zoff1 = PathPoints(i).coord(3) - PathPoints(1).coord(3)
  68.         
  69.         x1 = CurvePoints(1).coord(1) + xoff1
  70.         y1 = CurvePoints(1).coord(2) + yoff1
  71.         z1 = CurvePoints(1).coord(3) + zoff1
  72.         For j = 2 To NumCurvePts
  73.             x2 = CurvePoints(j).coord(1) + xoff1
  74.             y2 = CurvePoints(j).coord(2) + yoff1
  75.             z2 = CurvePoints(j).coord(3) + zoff1
  76.             pline.AddSegment x1, y1, z1, x2, y2, z2
  77.             x1 = x2
  78.             y1 = y2
  79.             z1 = z2
  80.         Next j
  81.     Next i
  82.  
  83.     ' Create the translated images of the path.
  84.     xoff1 = PathPoints(1).coord(1) - PathPoints(1).coord(1)
  85.     yoff1 = PathPoints(1).coord(2) - PathPoints(1).coord(2)
  86.     zoff1 = PathPoints(1).coord(3) - PathPoints(1).coord(3)
  87.     For i = 2 To NumPathPts
  88.         ' Calculate offsets for this path point.
  89.         xoff2 = PathPoints(i).coord(1) - PathPoints(1).coord(1)
  90.         yoff2 = PathPoints(i).coord(2) - PathPoints(1).coord(2)
  91.         zoff2 = PathPoints(i).coord(3) - PathPoints(1).coord(3)
  92.         
  93.         For j = 1 To NumCurvePts
  94.             pline.AddSegment _
  95.                 CurvePoints(j).coord(1) + xoff1, _
  96.                 CurvePoints(j).coord(2) + yoff1, _
  97.                 CurvePoints(j).coord(3) + zoff1, _
  98.                 CurvePoints(j).coord(1) + xoff2, _
  99.                 CurvePoints(j).coord(2) + yoff2, _
  100.                 CurvePoints(j).coord(3) + zoff2
  101.         Next j
  102.         xoff1 = xoff2
  103.         yoff1 = yoff2
  104.         zoff1 = zoff2
  105.     Next i
  106. End Sub
  107.  
  108. ' ***********************************************
  109. ' Return a string indicating the object type.
  110. ' ***********************************************
  111. Property Get ObjectType() As String
  112.     ObjectType = "EXTRUSION"
  113. End Property
  114.  
  115.  
  116.  
  117. ' ***********************************************
  118. ' Fix the data coordinates at their transformed
  119. ' values.
  120. ' ***********************************************
  121. Public Sub FixPoints()
  122. Dim i As Integer
  123. Dim j As Integer
  124.  
  125.     ' Fix the curve points.
  126.     For i = 1 To NumCurvePts
  127.         For j = 1 To 3
  128.             CurvePoints(i).coord(j) = CurvePoints(i).trans(j)
  129.         Next j
  130.     Next i
  131.  
  132.     ' Fix the path points.
  133.     For i = 1 To NumPathPts
  134.         For j = 1 To 3
  135.             PathPoints(i).coord(j) = PathPoints(i).trans(j)
  136.         Next j
  137.     Next i
  138.  
  139.     ' Fix the display polyline if it exists.
  140.     If Not pline Is Nothing Then pline.FixPoints
  141. End Sub
  142.  
  143. ' ************************************************
  144. ' Apply a transformation matrix which may not
  145. ' contain 0, 0, 0, 1 in the last column to the
  146. ' object.
  147. ' ************************************************
  148. Public Sub ApplyFull(M() As Single)
  149. Dim i As Integer
  150.  
  151.     ' Transform the curve.
  152.     For i = 1 To NumCurvePts
  153.         m3ApplyFull CurvePoints(i).coord, M, _
  154.                     CurvePoints(i).trans
  155.     Next i
  156.     
  157.     ' Transform the path.
  158.     For i = 1 To NumPathPts
  159.         m3ApplyFull PathPoints(i).coord, M, _
  160.                     PathPoints(i).trans
  161.     Next i
  162.     
  163.     ' Transform the display polyline if it exists.
  164.     If Not pline Is Nothing Then pline.ApplyFull M
  165. End Sub
  166.  
  167. ' ************************************************
  168. ' Apply a transformation matrix to the object.
  169. ' ************************************************
  170. Public Sub Apply(M() As Single)
  171. Dim i As Integer
  172.  
  173.     ' Transform the curve.
  174.     For i = 1 To NumCurvePts
  175.         m3Apply CurvePoints(i).coord, M, _
  176.                 CurvePoints(i).trans
  177.     Next i
  178.     
  179.     ' Transform the path.
  180.     For i = 1 To NumPathPts
  181.         m3Apply PathPoints(i).coord, M, _
  182.                 PathPoints(i).trans
  183.     Next i
  184.     
  185.     ' Transform the display polyline if it exists.
  186.     If Not pline Is Nothing Then pline.Apply M
  187. End Sub
  188.  
  189.  
  190. ' ************************************************
  191. ' Apply a nonlinear transformation.
  192. ' ************************************************
  193. Public Sub Distort(D As Object)
  194. Dim i As Integer
  195.  
  196.     ' Distort the curve.
  197.     For i = 1 To NumCurvePts
  198.         D.Distort CurvePoints(i).coord(1), _
  199.                   CurvePoints(i).coord(2), _
  200.                   CurvePoints(i).coord(3)
  201.     Next i
  202.     
  203.     ' Distort the path.
  204.     For i = 1 To NumPathPts
  205.         D.Distort PathPoints(i).coord(1), _
  206.                   PathPoints(i).coord(2), _
  207.                   PathPoints(i).coord(3)
  208.     Next i
  209.     
  210.     ' Distort the display polyline if it exists.
  211.     If Not pline Is Nothing Then pline.Distort D
  212. End Sub
  213.  
  214.  
  215. ' ************************************************
  216. ' Write the surface's display polyline object to a
  217. ' file using Write. The data can later be loaded
  218. ' into an ObjPolyline object but not an
  219. ' ObjExtrusion object.
  220. ' ************************************************
  221. Public Sub FileWritePolyline(filenum As Integer)
  222.     If Not pline Is Nothing Then pline.FileWrite filenum
  223. End Sub
  224.  
  225.  
  226. ' ************************************************
  227. ' Write an extruded surface to a file using Write.
  228. ' Begin with "EXTRUSION" to identify this object.
  229. ' ************************************************
  230. Public Sub FileWrite(filenum As Integer)
  231. Dim i As Integer
  232.  
  233.     ' Write basic information.
  234.     Write #filenum, _
  235.         "EXTRUSION", NumCurvePts, NumPathPts
  236.         
  237.     ' Write the curve points.
  238.     For i = 1 To NumCurvePts
  239.         Write #filenum, _
  240.             CurvePoints(i).coord(1), _
  241.             CurvePoints(i).coord(2), _
  242.             CurvePoints(i).coord(3)
  243.     Next i
  244.     
  245.     ' Write the path points.
  246.     For i = 1 To NumPathPts
  247.         Write #filenum, _
  248.             PathPoints(i).coord(1), _
  249.             PathPoints(i).coord(2), _
  250.             PathPoints(i).coord(3)
  251.     Next i
  252. End Sub
  253.  
  254.  
  255.  
  256.  
  257. ' ************************************************
  258. ' Draw the extrusion on a Form, Printer, or
  259. ' PictureBox.
  260. ' ************************************************
  261. Public Sub Draw(canvas As Object, Optional R As Variant)
  262.     If Not pline Is Nothing Then _
  263.         pline.Draw canvas, R
  264. End Sub
  265.  
  266.  
  267. ' ************************************************
  268. ' Read a grid from a file using Input.
  269. ' Assume the "EXTRUSION" label has already been
  270. ' read.
  271. ' ************************************************
  272. Public Sub FileInput(filenum As Integer)
  273. Dim i As Integer
  274.  
  275.     ' Get the basic information.
  276.     Input #filenum, NumCurvePts, NumPathPts
  277.     
  278.     ' Allocate the curve and path arrays.
  279.     ReDim CurvePoints(1 To NumCurvePts)
  280.     ReDim PathPoints(1 To NumPathPts)
  281.     
  282.     ' Read the curve points.
  283.     For i = 1 To NumCurvePts
  284.         Input #filenum, _
  285.             CurvePoints(i).coord(1), _
  286.             CurvePoints(i).coord(2), _
  287.             CurvePoints(i).coord(3)
  288.         CurvePoints(i).coord(4) = 1
  289.     Next i
  290.  
  291.     ' Read the path points.
  292.     For i = 1 To NumPathPts
  293.         Input #filenum, _
  294.             PathPoints(i).coord(1), _
  295.             PathPoints(i).coord(2), _
  296.             PathPoints(i).coord(3)
  297.         PathPoints(i).coord(4) = 1
  298.     Next i
  299.  
  300.     ' Create the display polyline.
  301.     Extrude
  302. End Sub
  303.  
  304.  
  305.